package com.etime.shycourse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.dto.CourseAndCommentDto;
import com.etime.shycourse.dto.CoursecommentDto;
import com.etime.shycourse.pojo.Coursecomment;
import com.etime.shycourse.services.interfaces.CoursecommentServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wb
 * @since 2022-03-02
 */
@Api(tags = "课程评价-管理数据接口")
@RestController
@RequestMapping("/sys/courseComment")
public class CoursecommentController {
    @Autowired
    private CoursecommentServices service;

    @ApiOperation("后台管理课程评论信息接口")
    @ApiImplicitParam("据课程评论的id 进行修改 评论的置顶，加精")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentid", value = "必传值 评论的id"),
            @ApiImplicitParam("没有中文注释的，根据前端的需要传值"),
    })
    @PostMapping("/update")
    public BaseMessage updateCommentById(@RequestBody Coursecomment coursecomment) {
        BaseMessage msg = new BaseMessage ();
        if (coursecomment == null) {
            msg.setCode (-2);
            msg.setMsg ("修改课程为空");
        }
        Integer rtn = service.updateCommentById (coursecomment);
        msg.setCode (rtn);
        if (rtn != null && rtn > 0) {
            msg.setMsg ("修改课程评论成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("修改课程评论失败");
        }
        return msg;
    }

    @ApiOperation("后台管理课程评论条数接口")
    @ApiImplicitParam("根据课程id 统计评论条数")
    @GetMapping("/course/count/{CourseId}")
    public BaseMessage countComment(@PathVariable Integer CourseId) {
        int rtn = service.countComment (CourseId);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("统计该课程评论条数成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("统计该课程评论条数失败");
        }
        return msg;
    }

    @ApiOperation("后台管理课程提问条数接口")
    @ApiImplicitParam("根据课程id 统计提问条数")
    @GetMapping("/questions/count/{courseId}")
    public BaseMessage countQuestion(@PathVariable Integer courseId) {
        int rtn = service.countQuestions (courseId);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("统计该课程提问条数成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("统计该课程提问条数失败");
        }
        return msg;
    }

    @ApiOperation("根据课程id查询课程所有的评论")
    @GetMapping("/list")
    public ListMessage<Coursecomment> selectSysCommentList(Coursecomment Coursecomment, Long pagenum, Long pagesize) {
        ListMessage<Coursecomment> msg = new ListMessage<> ();
        List<Coursecomment> data = service.selectSysCommentList (Coursecomment);
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到课程的评论");
        }
        return msg;
    }


    @ApiOperation("一个课程，根据课程id查询课程所有的评论 分页")
    @GetMapping("/page")
    public PageMessage<Coursecomment> selectSysCommentPage(Coursecomment Coursecomment, Long pagenum, Long pagesize) {
        PageMessage<Coursecomment> msg = new PageMessage<> ();
        Page<Coursecomment> data = service.selectSysCommentPage (Coursecomment, pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到课程的评论");
        }
        return msg;
    }

    @ApiOperation("所有课程,根据课程id查询课程所有的评论分页")
    @GetMapping("/pages")
    public PageMessage<CoursecommentDto> selectSysCommentPages(CoursecommentDto Coursecomment, Long pagenum, Long pagesize) {
        PageMessage<CoursecommentDto> msg = new PageMessage<> ();
        Page<CoursecommentDto> data = service.selectSysCommentPages (Coursecomment, pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到课程的评论");
        }
        return msg;
    }

    // 后台不用分页 弃用
    @ApiOperation("根据评论id查询回复")
    @GetMapping("/get/{commentId}/{pagenum}/{pagesize}")
    public PageMessage<Coursecomment> selectSysCommentByIDs(@PathVariable Integer commentId, @PathVariable Long pagenum, @PathVariable Long pagesize) {
        PageMessage<Coursecomment> msg = new PageMessage<> ();
        Page<Coursecomment> data = service.selectSysCommentByID (commentId, pagenum, pagesize);
        if (data != null) {
            msg.setCode (1);
            msg.setPagedata (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到评论");
        }
        return msg;
    }

    @ApiOperation("根据评论id查询回复")
    @GetMapping("/gets/{commentId}")
    public ListMessage<Coursecomment> insertChapter(@PathVariable Integer commentId) {
        ListMessage<Coursecomment> msg = new ListMessage<> ();
        List<Coursecomment> data = service.selectSysCommentByID (commentId);
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到评论");
        }
        return msg;
    }


    @ApiOperation("对已经插入评论 进行评价(回复) 评论：1 默认  commenttype= 1 -->评论")
    @PostMapping("/insert")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentpid", value = "必传 评论的id"),
    })
    public PojoMessage insetSysCourseComment(Coursecomment coursecomment, HttpServletRequest request) {
        PojoMessage msg = new PojoMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        int integer = service.insetSysCourseComment (coursecomment, token);
        if (integer == 0) {
            msg.setCode (0);
            msg.setMsg ("回复失败");
        } else {
            msg.setCode (integer);
            msg.setMsg ("回复成功");
            msg.setData (coursecomment);
        }
        return msg;
    }

    @ApiOperation("后台 ,根据评论id查询,课程信息+统计评论的条数")
    @GetMapping("/get/Numbers/{CourseId}")
    public PojoMessage<CourseAndCommentDto> selectCourseAndCommentDtoByIds(@PathVariable Integer CourseId) {
        PojoMessage<CourseAndCommentDto> msg = new PojoMessage<> ();
        CourseAndCommentDto data = service.selectCourseAndCommentDtoByIds (CourseId);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到数据");
        }
        return msg;
    }
}


